<?php

namespace app\order\controller;

use app\common\controller\PCBase;
use app\common\controller\DBase;
use app\order\logic\OrderLogic;
use app\order\logic\OrderShipLogic;
use app\order\logic\OrderServiceLogic;
use app\order\logic\OrderWebHookLogic;
use app\order\model\Order as OrderModel;
use think\facade\Db;
use Throwable;
use app\common\library\Upload;

class Order extends PCBase
{
    /**
     * 模型
     * @var object
     * @phpstan-var OrderModel
     */
    protected object $model;

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new OrderModel();
    }

    /**
     * 查看test
     * @throws Throwable
     */
    public function index()
    {
        $params     = $this->request->post();
        $orderLogic = new OrderLogic();
        return $orderLogic->getOrdersList($params);
    }

    /**
     * 编辑
     * @throws Throwable
     */
    public function edit($id = null)
    {
        $row = $this->model->find($id);
        if (!$row) {
            return json(DefaultResponse(201, false, '记录不能为空!', null));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->modelValidate) {
                try {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = new $validate();
                    $validate->scene('edit')->check($data);
                } catch (Throwable $e) {
                    return json(DefaultResponse(201, false, $e->getMessage(), null));
                }
            }

            $orderLogic = new OrderLogic();
            $result = $orderLogic->editOrder($row, $data);

            return $result;
        }

        return json(DefaultResponse(200, true, '获取成功', ['row' => $row]));
    }

    /**
     * 自动发货
     *
     */
    public function automaticShipping()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderShipLogic();
        return $orderShipLogic->automaticShipping($params);
    }


    /**
     * 强制发货
     */
    public function mandatoryShipping()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderShipLogic();
        return $orderShipLogic->mandatoryShipping($params);
    }

    /**
     * 标记顺丰TK面单
     * @return \think\response\Json
     */
    public function processedShunfeng()
    {
        $params = $this->request->post();
        $id     = $params['ids'];
        $ids    = is_array($id) ? $id : explode(',', $id);
        $items  = Db::name('order')->whereIn("id",$ids)->select();
        $items  = objectToArray($items);
        if($items){
            //批量获取order_ids
            $order_ids = array_values(array_unique(array_column($items,'order_id')));
            Db::name('order_ship')->whereIn('order_id', $order_ids)->update(['type'=>2]);
        }
        return json(DefaultResponse(200, true, '操作成功'));
    }

    /**
     * 上传刷单文件
     */
    public function uploadBrushOrder()
    {

        $file       = $this->request->file('file');
        $upload     = new Upload($file);
        $attachment = $upload->upload(null, $this->getAdminId());
        $orderLogic = new OrderLogic();
        $return     = $orderLogic->insertBrushOrder($attachment['url']);
        return  $this->jsonMessage(200, true, $return,null);
    }

    /**
     * 订单导出
     */
    public function export()
    {
        $params = $this->request->post();
        $orderLogic = new OrderLogic();
        return $orderLogic->exportOrder($params);
    }





    /**
     * @desc    订单状态webhook回调接口消费
     * @atuthor  luckyXiong
     * @date     2024-04-29
     * @action:  定时去调用
     */
    public function orderStatusUpdateByHookConsummer(){
        $data   = $this->request->post();
        return (new OrderWebHookLogic())->orderStatusChange($data);
    }



    /**
     * @desc     获取仓库列表信息
     * @atuthor  luckyXiong
     * @date     2024-04-01
     */
    public function getWareHouseList(){
        $data   = $this->request->post();
        return (new OrderLogic())->getWareHouseList($data);
    }


    /**
     * @desc  发货根据物流商ID获取下拉的仓库ID
     * @atuthor  luckyXiong
     * @date  2024-03-28
     */
    public function getGoldenByWarehouse(){
        $data   = $this->request->post();
        return (new OrderLogic())->getGoldenByWarehouse($data);
    }


    /**
     * @desc     是否刷单下拉列表
     * @atuthor  luckyXiong
     * @date     2024-04-08
     */
    public function getBrushlist(){
        $data   = $this->request->post();
        return (new OrderLogic())->getBrushlist($data);
    }








}
